using System;
using Microsoft.Health.Web;
using Microsoft.Health.PatientConnect;
using Microsoft.Health.Samples.PatientConnect.TreyResearch.HealthVaultAdapter;
using Microsoft.Health;
using System.Data;
using System.Collections.Generic;

/// <summary>
/// Utility methods wrapper over the PatientConnect API
/// </summary>
public class PatientConnectUtilities
{
    Guid _applicationId = new Guid();
    /// <summary>
    /// Patient Connect Utility class
    /// </summary>
    /// <param name="AppId">Application Id</param>
    public PatientConnectUtilities(Guid appId)
    {
        _applicationId = appId;
    }

    /// <summary>
    /// Creates new application Id (Child AppId)for practice
    /// </summary>
    /// <param name="applicationInfo">AppConfigInfo class</param>
    /// <returns>newly generated application Id</returns>
    public Guid CreateApplication(ApplicationInfo applicationInfo)
    {
        Guid childAppID = new Guid();
        OfflineWebApplicationConnection offlineConn = HealthVaultConnectionManager.CreateConnection(_applicationId);
        childAppID = Microsoft.Health.ApplicationProvisioning.Provisioner.AddApplication(offlineConn, applicationInfo);
        return childAppID;
    }

    /// <summary>
    /// Create patient in Health vault
    /// </summary>
    /// <param name="friendlyName">Patient's Name</param>
    /// <param name="securityQuestion">Security question</param>
    /// <param name="securityAnswer">Security Answer</param>
    /// <param name="applicationPatientId">Patient Application ID</param>
    /// <returns>Returns 20 digit identity code</returns>
    public string CreatePatient(string friendlyName,
                                string securityQuestion,
                                string securityAnswer,
                                string applicationPatientId)
    {
        OfflineWebApplicationConnection offlineConnection = HealthVaultConnectionManager.CreateConnection(_applicationId);
        string identityCode = string.Empty;
        identityCode = PatientConnection.Create(offlineConnection, friendlyName, securityQuestion,
            securityAnswer, null, applicationPatientId);
        return identityCode;
    }
    /// <summary>
    /// Create List of authorization rules
    /// </summary>
    /// <param name="authroizationTable">authorization table from Grid View</param>
    /// <returns>List of AuthorizationRule</returns>
    public static List<AuthorizationRule> SetAuthorizationRule(DataTable authroizationTable)
    {
        List<AuthorizationRule> authorizationRuleList = new List<AuthorizationRule>();
        foreach (DataRow dr in authroizationTable.Select())
        {
            List<AuthorizationSetDefinition> authsetDefinition = new List<AuthorizationSetDefinition>();
            AuthorizationRule authrule;
            if (!(dr[1].Equals(false) && dr[2].Equals(false) && dr[3].Equals(false) && dr[4].Equals(false)))
            {
                if (dr["Create"].Equals(true))
                {
                    authsetDefinition = new List<AuthorizationSetDefinition>();
                    authsetDefinition.Add(new TypeIdSetDefinition(new Guid(dr["TypeId"].ToString())));
                    authrule = new AuthorizationRule(HealthRecordItemPermissions.Create, authsetDefinition, new List<AuthorizationSetDefinition>());
                    authorizationRuleList.Add(authrule);
                }
                if (dr["Read"].Equals(true))
                {
                    authsetDefinition = new List<AuthorizationSetDefinition>();
                    authsetDefinition.Add(new TypeIdSetDefinition(new Guid(dr["TypeId"].ToString())));
                    authrule = new AuthorizationRule(HealthRecordItemPermissions.Read, authsetDefinition, new List<AuthorizationSetDefinition>());
                    authorizationRuleList.Add(authrule);
                }
                if (dr["Update"].Equals(true))
                {
                    authsetDefinition = new List<AuthorizationSetDefinition>();
                    authsetDefinition.Add(new TypeIdSetDefinition(new Guid(dr["TypeId"].ToString())));
                    authrule = new AuthorizationRule(HealthRecordItemPermissions.Update, authsetDefinition, new List<AuthorizationSetDefinition>());
                    authorizationRuleList.Add(authrule);
                }
                if (dr["Delete"].Equals(true))
                {
                    authsetDefinition = new List<AuthorizationSetDefinition>();
                    authsetDefinition.Add(new TypeIdSetDefinition(new Guid(dr["TypeId"].ToString())));
                    authrule = new AuthorizationRule(HealthRecordItemPermissions.Delete, authsetDefinition, new List<AuthorizationSetDefinition>());
                    authorizationRuleList.Add(authrule);
                }
            }
        }
        return authorizationRuleList;
    }
}
